<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="55%" class="customer-form-dialog">
    <div class="dialog-content">
      <!-- 头部区域 -->
      <div class="dialog-header">
        <div class="header-icon">
          <Icon icon="ep:user-filled" />
        </div>
        <div class="header-content">
          <h3 class="dialog-title">{{ dialogTitle }}</h3>
          <p class="dialog-subtitle">配置客户基本信息</p>
        </div>
      </div>

      <!-- 表单区域 -->
      <div class="form-container">
        <el-form
          ref="formRef"
          v-loading="formLoading"
          :model="formData"
          :rules="formRules"
          label-width="100px"
          :inline="true"
          class="enhanced-form"
        >
          <!-- 所属模块 -->
          <div class="module-section">
            <div class="section-header">
              <Icon icon="ep:grid" class="section-icon" />
              <span class="section-title">所属模块</span>
            </div>
            <el-form-item prop="moduleId" class="module-item">
              <el-radio-group
                v-model="formData.moduleId"
                is-button
                @change="onChangeSetModuleId"
                class="module-radio-group"
              >
                <el-radio-button
                  v-for="item in modules"
                  :key="item.id"
                  :label="item.id"
                  class="module-radio-btn"
                >
                  {{ item.name }}
                </el-radio-button>
              </el-radio-group>
            </el-form-item>
          </div>

          <!-- 基本信息 -->
          <div class="basic-section">
            <div class="section-header">
              <Icon icon="ep:document" class="section-icon" />
              <span class="section-title">基本信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="客户名称" prop="name" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:office-building" class="input-icon" />
                  <el-input
                    v-model="formData.name"
                    placeholder="请输入客户名称"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="客户来源" prop="source" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:connection" class="select-icon" />
                  <el-select
                    v-model="formData.source"
                    placeholder="请选择客户来源"
                    class="enhanced-select"
                  >
                    <el-option
                      v-for="dict in getIntDictOptions(DICT_TYPE.CRM_CUSTOMER_SOURCE)"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item label="手机" prop="mobile" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:phone" class="input-icon" />
                  <el-input
                    v-model="formData.mobile"
                    placeholder="请输入手机"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="负责人" prop="ownerUserId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:user" class="select-icon" />
                  <el-select
                    filterable
                    v-model="formData.ownerUserId"
                    :disabled="formType !== 'create'"
                    class="enhanced-select"
                    placeholder="请选择负责人"
                  >
                    <el-option
                      v-for="item in userOptions"
                      :key="item.id"
                      :label="item.nickname"
                      :value="item.id"
                    />
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item label="电话" prop="telephone" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:phone" class="input-icon" />
                  <el-input
                    v-model="formData.telephone"
                    placeholder="请输入电话"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="邮箱" prop="email" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:message" class="input-icon" />
                  <el-input
                    v-model="formData.email"
                    placeholder="请输入邮箱"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="微信" prop="wechat" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:chat-dot-round" class="input-icon" />
                  <el-input
                    v-model="formData.wechat"
                    placeholder="请输入微信"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>

              <el-form-item label="QQ" prop="qq" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:chat-line-round" class="input-icon" />
                  <el-input v-model="formData.qq" placeholder="请输入 QQ" class="enhanced-input" />
                </div>
              </el-form-item>

              <el-form-item label="客户行业" prop="industryId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:office-building" class="select-icon" />
                  <el-select
                    v-model="formData.industryId"
                    placeholder="请选择客户行业"
                    class="enhanced-select"
                  >
                    <el-option
                      v-for="dict in getIntDictOptions(DICT_TYPE.CRM_CUSTOMER_INDUSTRY)"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item label="客户级别" prop="levelId" class="form-item">
                <div class="select-wrapper">
                  <Icon icon="ep:star" class="select-icon" />
                  <el-select
                    v-model="formData.levelId"
                    placeholder="请选择客户级别"
                    class="enhanced-select"
                  >
                    <el-option
                      v-for="dict in resultLevel"
                      :key="dict.id"
                      :label="dict.name"
                      :value="dict.id"
                    >
                      <el-tag
                        :disable-transitions="false"
                        :style="{
                          background: dict.bgColor,
                          color: dict.textColor,
                          marginRight: '10px',
                          border: 'none',
                          cursor: 'pointer',
                          paddingRight: '9px'
                        }"
                      >
                        {{ dict.name }}
                      </el-tag>
                    </el-option>
                  </el-select>
                </div>
              </el-form-item>

              <el-form-item label="地址" prop="areaId" class="form-item">
                <div class="cascader-wrapper">
                  <el-cascader
                    @change="handleChange"
                    ref="cascaderRef"
                    v-model="formData.areaId"
                    :options="areaList"
                    :props="defaultPropsCheck"
                    class="enhanced-cascader"
                    clearable
                    filterable
                    placeholder="请选择城市"
                  />
                </div>
              </el-form-item>

              <el-form-item label="详细地址" prop="detailAddress" class="form-item">
                <div class="input-wrapper">
                  <Icon icon="ep:map-location" class="input-icon" />
                  <el-input
                    v-model="formData.detailAddress"
                    placeholder="请输入详细地址"
                    class="enhanced-input"
                  />
                </div>
              </el-form-item>
            </div>
          </div>

          <!-- 自定义字段 -->
          <div class="custom-section" v-if="formFiled.length > 0">
            <div class="section-header">
              <Icon icon="ep:setting" class="section-icon" />
              <span class="section-title">自定义字段</span>
            </div>

            <div class="form-grid">
              <el-form-item
                v-for="item in formFiled"
                :key="item.id"
                :label="item.name"
                class="form-item"
                :required="item.isRequired"
              >
                <!-- 文本 -->
                <template v-if="item.fieldType == 'input'">
                  <div class="input-wrapper">
                    <Icon icon="ep:edit" class="input-icon" />
                    <el-input
                      class="enhanced-input"
                      v-model="formDataFile[item.fieldName]"
                      :placeholder="'请输入' + item.name"
                    />
                  </div>
                </template>

                <!-- select -->
                <template v-if="item.fieldType == 'select'">
                  <div class="select-wrapper">
                    <Icon icon="ep:arrow-down" class="select-icon" />
                    <el-select
                      v-model="formDataFile[item.fieldName]"
                      class="enhanced-select"
                      :placeholder="'请选择' + item.name"
                    >
                      <el-option
                        v-for="(option, idx) in item.options"
                        :key="idx"
                        :label="option.label"
                        :value="option.value"
                      />
                    </el-select>
                  </div>
                </template>

                <!--number  -->
                <template v-if="item.fieldType == 'number'">
                  <div class="number-wrapper">
                    <Icon icon="ep:sort" class="number-icon" />
                    <el-input-number
                      v-model="formDataFile[item.fieldName]"
                      :min="1"
                      class="enhanced-number"
                    />
                  </div>
                </template>

                <!-- time -->
                <template v-if="item.fieldType == 'time'">
                  <div class="date-wrapper">
                    <Icon icon="ep:calendar" class="date-icon" />
                    <el-date-picker
                      v-model="formDataFile[item.fieldName]"
                      :placeholder="'请选择' + item.name"
                      type="datetime"
                      class="enhanced-date-picker"
                      value-format="x"
                    />
                  </div>
                </template>
              </el-form-item>
            </div>
          </div>

          <!-- 其他信息 -->
          <div class="other-section">
            <div class="section-header">
              <Icon icon="ep:more" class="section-icon" />
              <span class="section-title">其他信息</span>
            </div>

            <div class="form-grid">
              <el-form-item label="下次联系时间" prop="contactNextTime" class="form-item">
                <div class="date-wrapper">
                  <Icon icon="ep:calendar" class="date-icon" />
                  <el-date-picker
                    v-model="formData.contactNextTime"
                    placeholder="选择下次联系时间"
                    type="datetime"
                    class="enhanced-date-picker"
                    value-format="x"
                  />
                </div>
              </el-form-item>

              <el-form-item label="备注" prop="remark" class="form-item textarea-item">
                <div class="textarea-wrapper">
                  <Icon icon="ep:document" class="textarea-icon" />
                  <el-input
                    type="textarea"
                    v-model="formData.remark"
                    placeholder="请输入备注"
                    class="enhanced-textarea"
                    :rows="3"
                  />
                </div>
              </el-form-item>
            </div>
          </div>

          <!-- 标签 -->
          <div class="label-section" v-if="formType == 'create'">
            <div class="section-header">
              <Icon icon="ep:price-tag" class="section-icon" />
              <span class="section-title">标签</span>
            </div>

            <el-form-item prop="labelList" class="label-item">
              <div class="label-content">
                <div class="label-tags">
                  <el-tag
                    :style="{
                      background: init.bgColor,
                      color: init.textColor,
                      marginRight: '8px',
                      marginBottom: '8px'
                    }"
                    v-for="(init, index) in queryParams.labelList"
                    closable
                    class="label-tag"
                    @close="closeTags(init, index)"
                    :key="init.id"
                  >
                    {{ init.name }}
                  </el-tag>
                </div>

                <div class="label-actions">
                  <el-button :icon="Plus" size="small" @click="addPlus" class="add-label-btn">
                    <Icon icon="ep:plus" class="btn-icon" />
                    添加标签
                  </el-button>

                  <el-tooltip
                    v-if="queryParams.labelList.length"
                    class="box-item"
                    effect="dark"
                    content="清除全部"
                    placement="bottom-start"
                  >
                    <el-button
                      :icon="Delete"
                      @click="queryParams.labelList = []"
                      size="small"
                      class="clear-label-btn"
                    >
                      <Icon icon="ep:delete" class="btn-icon" />
                      清除全部
                    </el-button>
                  </el-tooltip>
                </div>
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button :disabled="formLoading" type="primary" @click="submitForm" class="confirm-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确定
        </el-button>
      </div>
    </template>
  </Dialog>

  <!-- 标签弹窗 -->
  <EditTagForm ref="editTagForm" @setlabel-list="setlabelList" :module-id="formData.moduleId" />
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as CustomerApi from '@/api/crm/customer'
import * as AreaApi from '@/api/system/area'
import { defaultPropsCheck } from '@/utils/tree'
import * as UserApi from '@/api/system/user'
import * as CompanyApi from '@/api/ent/company'
import { Plus, Delete } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'
import EditTagForm from './editTagForm/index.vue'
import * as LevelApi from '@/api/crm/customerTagConfig'
import { customFieldHeadList } from '@/api/crm/module/index'
import { emitter } from '@/utils/eventBus.ts'

import { useCrmStore } from '@/store/modules/crm'
const crmStore = useCrmStore()
// 从 store 中获取模块列表
const modules = ref([])
// 点击时更新当前模块id
const onChangeSetModuleId = (e) => {
  crmStore.setModuleId(e)
  formData.value.moduleId = e
  formData.value.levelId = null
  resultLevel.value = levelOptions.value.filter((i) => i.moduleId == e)
  getFiled()
}
const formDataFile = ref({})
const formFiled = ref([])
// 自定义字段
const getFiled = async () => {
  const fileds = await customFieldHeadList({
    moduleId: formData.value.moduleId,
    tableType: '2'
  })
  fileds.forEach((it) => {
    if (it.expansion)
      it.options = it.expansion.split(',').map((item) => ({ label: item, value: item }))
  })
  formFiled.value = fileds
}

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const cascaderRef = ref(null)
const handleChange = () => {
  cascaderRef.value.togglePopperVisible(false)
}

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const areaList = ref([]) // 地区列表
const levelOptions = ref([]) //客户级别
const resultLevel = ref([])
const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
const formData = ref({
  id: undefined,
  name: '未命名',
  contactNextTime: undefined,
  ownerUserId: 0,
  mobile: undefined,
  telephone: undefined,
  qq: undefined,
  wechat: undefined,
  email: undefined,
  areaId: undefined,
  detailAddress: undefined,
  industryId: undefined,
  levelId: undefined,
  source: undefined,
  remark: undefined,
  labelList: [],
  moduleId: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
  ownerUserId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
  moduleId: [{ required: true, message: '所属模块不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

const openByEnt = async (id?: string, number?: string) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.create')
  formType.value = 'create'
  resetForm()
  // 获得客户级别
  levelOptions.value = resultLevel.value = await LevelApi.getLevelSimple()
  if (id) {
    formLoading.value = true
    try {
      const data = await CompanyApi.getSimpleData({ cid: id })
      const isEmail = number.indexOf('@') != -1
      formData.value = {
        name: data.companyName,
        mobile: isEmail ? '' : number,
        telephone: isEmail ? '' : number,
        email: isEmail ? number : '',
        detailAddress: data.regAddr,
        source: 11,
        entId: id
      }
    } finally {
      formLoading.value = false
    }
  } else {
    formData.value.mobile = number
  }
  // 获得地区列表
  areaList.value = await AreaApi.getAreaTree()
  // 获得用户列表
  userOptions.value = await UserApi.getSimpleUserList()
  // 默认新建时选中自己
  if (formType.value === 'create') {
    formData.value.ownerUserId = useUserStore().getUser.id
  }
  modules.value = await crmStore.asyncGetModules()
}
/**回显*/
const setFieValue = async () => {
  await getFiled()
  formFiled.value.map((item) => {
    formDataFile.value[item.fieldName] = formData.value[item.fieldName]
  })
}
/** 打开弹窗 */
const open = async (type: string, id?: number, num, name) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.moduleId = crmStore.getModuleId
  if (formData.value.moduleId && type == 'create') {
    getFiled()
  }
  if (name) formData.value.name = name
  // 获得客户级别
  levelOptions.value = resultLevel.value = await LevelApi.getLevelSimple()
  if (num) formData.value.mobile = num
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CustomerApi.getCustomer(id)
      setFieValue()
      resultLevel.value = levelOptions.value.filter((i) => i.moduleId == formData.value.moduleId)
    } finally {
      formLoading.value = false
    }
  }
  // 获得地区列表
  areaList.value = await AreaApi.getAreaTree()
  // 获得用户列表
  userOptions.value = await UserApi.getSimpleUserList()
  // 默认新建时选中自己
  if (formType.value === 'create') {
    formData.value.ownerUserId = useUserStore().getUser.id
  }

  modules.value = await crmStore.asyncGetModules()
}
/*企业微信客户转客户**/
const weComOpen = async (type: string, item: Object) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  formData.value.moduleId = crmStore.getModuleId
  // 修改时，设置数据
  // formData.value = item
  formData.value.name = item.name
  formData.value.source = 301
  formData.value.weCustomerId = item.externalUserId
  // 获得地区列表
  areaList.value = await AreaApi.getAreaTree()
  // 获得用户列表
  userOptions.value = await UserApi.getSimpleUserList()
  // 默认新建时选中自己
  if (formType.value === 'create') {
    formData.value.ownerUserId = useUserStore().getUser.id
  }

  modules.value = await crmStore.asyncGetModules()
}
// ====== 标签
const queryParams = reactive({
  labelList: []
})
// 标签
const editTagForm = ref()
/*编辑标签*/

/*操作标签*/
const closeTags = (tag, index) => {
  queryParams.labelList.splice(index, 1)
}

// 添加搜索
const addPlus = () => {
  editTagForm.value.open('search', null, queryParams.labelList)
}
const setlabelList = (value) => {
  queryParams.labelList = value
}
// ===
defineExpose({ open, openByEnt, weComOpen }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true

  const arr = []
  let isRules = false
  formFiled.value.map((item) => {
    if (
      item.isRequired &&
      (formDataFile.value[item.fieldName] == undefined || formDataFile.value[item.fieldName] == '')
    )
      isRules = true
    arr.push({
      fieldHeadId: item.id,
      fieldName: item.fieldName,
      fieldDate: formDataFile.value[item.fieldName] || ''
    })
  })
  if (isRules) {
    ElMessage.warning('请检查必填项')
    formLoading.value = false
    return
  }

  try {
    if (queryParams.labelList.length > 0) {
      let ids = queryParams.labelList.map((v) => v.id)
      formData.value.labelList = ids
    }
    const data = formData.value as unknown as CustomerApi.CustomerVO
    data.customFieldList = arr
    let result
    if (formType.value === 'create') {
      result = await CustomerApi.createCustomer(data)
      message.success(t('common.createSuccess'))
    } else {
      result = await CustomerApi.updateCustomer(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', result)
    emitter.emit('refreshCustomer')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formDataFile.value = {}
  formData.value = {
    id: undefined,
    name: '未命名',
    contactNextTime: undefined,
    ownerUserId: 0,
    mobile: undefined,
    telephone: undefined,
    qq: undefined,
    wechat: undefined,
    email: undefined,
    areaId: undefined,
    detailAddress: undefined,
    industryId: undefined,
    levelId: undefined,
    source: undefined,
    remark: undefined,
    labelList: []
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
.customer-form-dialog {
  :deep(.el-dialog) {
    border-radius: 12px;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 20px auto;
      max-height: 90vh;
    }
  }

  :deep(.el-dialog__header) {
    display: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
    max-height: 80vh;
    overflow-y: auto;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: 1px solid #e5e7eb;
  }
}

.dialog-content {
  background: #f8fafc;
}

/* 对话框头部 */
.dialog-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px 24px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  color: white;
  border-radius: 10px;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }

  .form-item {
    width: 100% !important;
    margin-right: 0 !important;
    margin-bottom: 16px;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
      font-size: 14px;
      width: 100px;

      @media (max-width: 768px) {
        width: 80px;
        font-size: 13px;
      }
    }

    :deep(.el-form-item__content) {
      flex: 1;
    }

    &.textarea-item {
      grid-column: 1 / -1;
    }
  }
}

/* 区域样式 */
.module-section,
.basic-section,
.custom-section,
.other-section,
.label-section {
  margin-bottom: 24px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  border: 1px solid #f1f5f9;

  @media (max-width: 768px) {
    margin-bottom: 16px;
    padding: 16px;
  }
}

.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #0052cc;
}

.section-icon {
  color: #0052cc;
  font-size: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #374151;
}

/* 输入框样式 */
.input-wrapper,
.select-wrapper,
.cascader-wrapper,
.number-wrapper,
.date-wrapper,
.textarea-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.input-icon,
.select-icon,
.cascader-icon,
.number-icon,
.date-icon,
.textarea-icon {
  position: absolute;
  left: 12px;
  color: #6b7280;
  font-size: 16px;
  z-index: 1;
}

.enhanced-input,
.enhanced-select,
.enhanced-cascader,
.enhanced-number,
.enhanced-date-picker,
.enhanced-textarea {
  width: 100%;

  :deep(.el-input__wrapper) {
    padding-left: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    min-height: 40px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input__inner) {
    font-size: 14px;

    @media (max-width: 768px) {
      font-size: 16px; // 防止iOS缩放
    }
  }

  :deep(.el-input__suffix) {
    right: 12px;
  }
}

.enhanced-textarea {
  :deep(.el-textarea__inner) {
    padding-left: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    resize: vertical;
    min-height: 80px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &:focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.textarea-icon {
  top: 12px;
}

/* 模块选择 */
.module-item {
  width: 100%;
  margin-bottom: 0;

  :deep(.el-form-item__content) {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
}

.module-radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  width: 100%;

  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.module-radio-btn {
  border-radius: 6px;
  transition: all 0.2s ease;

  :deep(.el-radio-button__inner) {
    padding: 8px 16px;
    font-weight: 500;
    border-radius: 6px;

    @media (max-width: 768px) {
      padding: 10px 16px;
      width: 100%;
      text-align: center;
    }
  }

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0, 82, 204, 0.2);
  }
}

/* 标签区域 */
.label-item {
  width: 100%;
  margin-bottom: 0;

  :deep(.el-form-item__content) {
    width: 100%;
  }
}

.label-content {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.label-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 32px;
  padding: 8px;
  border: 1px dashed #d1d5db;
  border-radius: 6px;
  background: #f9fafb;
}

.label-tag {
  font-weight: 500;
  border-radius: 4px;
  transition: all 0.2s ease;

  &:hover {
    transform: scale(1.05);
  }
}

.label-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.add-label-btn,
.clear-label-btn {
  border-radius: 6px;
  font-size: 12px;
  padding: 6px 12px;
  transition: all 0.2s ease;

  .btn-icon {
    margin-right: 4px;
  }

  @media (max-width: 768px) {
    flex: 1;
  }
}

.add-label-btn {
  border: 1px solid #10b981;
  color: #10b981;

  &:hover {
    background: rgba(16, 185, 129, 0.05);
  }
}

.clear-label-btn {
  border: 1px solid #ef4444;
  color: #ef4444;

  &:hover {
    background: rgba(239, 68, 68, 0.05);
  }
}

/* 对话框底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 8px;
  }
}

.cancel-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  background: white;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.2s ease;
  min-height: 40px;

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }

  @media (max-width: 768px) {
    width: 100%;
    order: 2;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.confirm-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.2s ease;
  min-height: 40px;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    order: 1;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(4px);
}

:deep(.el-loading-spinner) {
  .circular {
    width: 32px;
    height: 32px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .dialog-content {
    background: #1f2937;
  }

  .form-container {
    background: #374151;
  }

  .module-section,
  .basic-section,
  .custom-section,
  .other-section,
  .label-section {
    background: #4b5563;
    border-color: #6b7280;
  }

  .dialog-title {
    color: #f9fafb;
  }

  .section-title {
    color: #f9fafb;
  }

  .section-header {
    border-color: #60a5fa;
  }

  .section-icon {
    color: #60a5fa;
  }

  .enhanced-input,
  .enhanced-select,
  .enhanced-cascader,
  .enhanced-number,
  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .enhanced-textarea {
    :deep(.el-textarea__inner) {
      background: #4b5563;
      border-color: #6b7280;
      color: #f9fafb;
    }
  }

  .input-icon,
  .select-icon,
  .cascader-icon,
  .number-icon,
  .date-icon,
  .textarea-icon {
    color: #d1d5db;
  }

  .label-tags {
    background: #4b5563;
    border-color: #6b7280;
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #f9fafb;
      }
    }
  }

  .dialog-footer {
    background: #374151;
  }

  :deep(.el-loading-mask) {
    background: rgba(55, 65, 81, 0.9);
  }
}

/* 高对比度模式适配 */
@media (prefers-contrast: high) {
  .enhanced-input,
  .enhanced-select,
  .enhanced-cascader,
  .enhanced-number,
  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      border: 2px solid #0052cc;
      box-shadow: none;

      &:hover,
      &.is-focus {
        border-color: #1d4ed8;
        box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
      }
    }
  }

  .enhanced-textarea {
    :deep(.el-textarea__inner) {
      border: 2px solid #0052cc;
      box-shadow: none;

      &:hover,
      &:focus {
        border-color: #1d4ed8;
        box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
      }
    }
  }

  .cancel-btn {
    border: 2px solid #6b7280;
  }

  .confirm-btn {
    background: #0052cc;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
}

/* 减少动画模式适配 */
@media (prefers-reduced-motion: reduce) {
  .enhanced-input,
  .enhanced-select,
  .enhanced-cascader,
  .enhanced-number,
  .enhanced-date-picker,
  .enhanced-textarea,
  .cancel-btn,
  .confirm-btn,
  .module-radio-btn,
  .label-tag {
    transition: none;
  }

  .confirm-btn {
    &:hover {
      transform: none;
    }
  }

  .module-radio-btn {
    &:hover {
      transform: none;
    }
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .enhanced-input,
  .enhanced-select,
  .enhanced-cascader,
  .enhanced-number,
  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      min-height: 44px;
    }
  }

  .cancel-btn,
  .confirm-btn {
    min-height: 44px;
    padding: 12px 20px;
  }

  .module-radio-btn {
    :deep(.el-radio-button__inner) {
      min-height: 44px;
      padding: 12px 16px;
    }
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px;
  }

  .form-container {
    padding: 12px;
  }

  .module-section,
  .basic-section,
  .custom-section,
  .other-section,
  .label-section {
    padding: 12px;
    margin-bottom: 12px;
  }

  .dialog-footer {
    padding: 12px;
  }

  .enhanced-form {
    .form-grid {
      gap: 12px;
    }
  }
}

/* 横屏模式适配 */
@media (max-width: 768px) and (orientation: landscape) {
  .dialog-header {
    padding: 12px 16px;
    flex-direction: row;
    text-align: left;
  }

  .form-container {
    padding: 16px;
  }

  .enhanced-form {
    .form-grid {
      grid-template-columns: repeat(2, 1fr);
      gap: 16px;
    }
  }

  .module-radio-group {
    flex-direction: row;
    flex-wrap: wrap;
  }
}
</style>
